﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace AIC_CD
{
    public partial class BaoCao01 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataToDropDownList(ddlKyBaoCao, "KyBaoCao");
                BindDataToDropDownList(ddlTenBaoCao, "BaoCaoDinhKem");
            }
            
        }

        protected void btnXemCiTiet_Click(object sender, EventArgs e)
        {
            try
            {
                int UserID = Convert.ToInt32(Session["userid"]);
                string sql = "INSERT INTO dbo.tblBaoCaoAttach( BaoCaoAttachID_DM ,KyBaoCaoID_DM ,DonVi_ID ,TenBaoCao ,NoiDung ,FileDinhKem ,CreateDate ,UpdateDate ,UserID) " +
                             "VALUES  ( @BaoCaoAttachID_DM ,@KyBaoCaoID_DM ,@DonVi_ID ,@TenBaoCao ,@NoiDung ,@FileDinhKem ,GETDATE() ,GETDATE() ,@UserID )";
                int BaoCaoAttachID_DM = Convert.ToInt32(ddlKyBaoCao.SelectedValue);
                int KyBaoCaoID_DM = Convert.ToInt32(ddlTenBaoCao.SelectedValue);
                string TenBaoCao = txtTenBaoCao.Text;
                string NoiDung = txtNoiDung.Value;
                string FileDinhKem = UploadFile();

                List<SqlParameter> arrParams = new List<SqlParameter>();
                arrParams.Add(SqlHelper.CreateParameterObject("@BaoCaoAttachID_DM", SqlDbType.Int, ParameterDirection.Input, BaoCaoAttachID_DM));
                arrParams.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, KyBaoCaoID_DM));
                arrParams.Add(SqlHelper.CreateParameterObject("@DonVi_ID", SqlDbType.Int, ParameterDirection.Input, Convert.ToInt32(MyCommon.GetDonViID(UserID.ToString()))));
                arrParams.Add(SqlHelper.CreateParameterObject("@TenBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, TenBaoCao));
                arrParams.Add(SqlHelper.CreateParameterObject("@NoiDung", SqlDbType.NVarChar, ParameterDirection.Input, NoiDung));
                arrParams.Add(SqlHelper.CreateParameterObject("@FileDinhKem", SqlDbType.NVarChar, ParameterDirection.Input, FileDinhKem));
                arrParams.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, UserID));
                SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
                lblNotice.Visible = true;
                lblNotice.InnerText = "Cập nhật thành công Báo cáo : " + TenBaoCao;
            }
            catch (Exception)
            {
                lblNotice.Visible = true;
                lblNotice.InnerText = "Cập nhật xảy ra lỗi";
            }
        }

        private string UploadFile()
        {
            try
            {
                string sFolderPath = Server.MapPath(@"userfiles/BaoCao");
                HttpPostedFile myFile = fulFileDinhKem.PostedFile;
                string sFileName = myFile.FileName;
                string[] arrFileName = sFileName.Split('.');
                arrFileName[0] = DateTime.Now.ToString("ddMMyyhhssmm");
                sFileName = arrFileName[0] + "." + arrFileName[1];
                myFile.SaveAs(string.Format(@"{0}\{1}", sFolderPath, sFileName));
                return "userfiles/BaoCao/" + sFileName;
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                return "";
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("index.aspx");
        }

        protected void BindDataToDropDownList(DropDownList BindDropDown, string MaDM)
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@MaDM", SqlDbType.NVarChar, ParameterDirection.Input, MaDM));
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, "SELECT * FROM dbo.tblDanhMuc WHERE MaDM=@MaDM", arrParams);
            BindDropDown.DataSource = dt;
            BindDropDown.DataTextField = "TenDM";
            BindDropDown.DataValueField = "ID";
            BindDropDown.DataBind();
        }
    }
}
